package cn.cntv.app.ipanda.home.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import cn.cntv.app.ipanda.BaseFragment;
import cn.cntv.app.ipanda.R;
import cn.cntv.app.ipanda.home.adapter.BannerAdapter;
import cn.cntv.app.ipanda.home.adapter.HomeAdapter;
import cn.cntv.app.ipanda.home.entity.Data;
import cn.cntv.app.ipanda.home.entity.GridData;
import cn.cntv.app.ipanda.home.entity.GroupData;
import cn.cntv.app.ipanda.home.entity.ItemData1;
import cn.cntv.app.ipanda.home.entity.ItemData2;
import cn.cntv.app.ipanda.home.entity.ItemData3;
import cn.cntv.app.ipanda.view.AutoScrollViewPager;
import cn.cntv.app.ipanda.view.PointView;

/**
 * @ClassName: HomeFragment
 * @author Xiao JinLai
 * @Date Dec 24, 2015 2:28:52 PM
 * @Description：首页 Fragment
 */
public class HomeFragment extends BaseFragment implements
		ViewPager.OnPageChangeListener {

	private HomeAdapter mAdapter;
	private ListView mListView;
	private AutoScrollViewPager viewPager;
	private BannerAdapter bannerAdapter;
	private PointView mPointView;

	private static final long INTERVAL = 5000;

	/**
	 * 实例化方法，如果传入参数必须实现setArguments
	 * 
	 * @return
	 */
	public static HomeFragment newInstance() {
		HomeFragment fragment = new HomeFragment();
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.fragment_home, container, false);
		mListView = (ListView) view.findViewById(R.id.listview);
		
		viewPager = (AutoScrollViewPager) view.findViewById(R.id.viewPager);
		viewPager.setOnPageChangeListener(this);
		viewPager.setInterval(INTERVAL);

		mPointView = (PointView) view.findViewById(R.id.pointview);
		initViewpager(inflater);
		
		return view;
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (mAdapter == null) {
			initAdapter();
		}
		mListView.setAdapter(mAdapter);
	}

	private void initViewpager(LayoutInflater mInflater) {
		
		List<View> views = new ArrayList<View>();
		
		if (bannerAdapter == null) {
			
			ImageView imageView;
			
			int[] ids = { R.drawable.img1, R.drawable.img2, R.drawable.img3,
					R.drawable.img4 };
			
			mPointView.setPointCount(ids.length);
			
			for (int i = 0, j = ids.length; i < j; i++) {
				
				imageView = (ImageView) mInflater.inflate(
						R.layout.view_banner_viewpager_imageview, null);
				imageView.setImageResource(ids[i]);
				views.add(imageView);
				imageView.setTag(String.valueOf(i));
				imageView.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// onViewPagerClick(v.getTag().toString());
					}
				});
			}
			
			bannerAdapter = new BannerAdapter(views);
			viewPager.setAdapter(bannerAdapter);
			viewPager.startAutoScroll();
			
		} else {
			
			bannerAdapter.setList(views);
		}
		mPointView.setCurrentIndex(0);
		viewPager.setCurrentItem(0);
	}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {

	}

	@Override
	public void onPageSelected(int position) {
		mPointView.setCurrentIndex(position);
	}

	@Override
	public void onPageScrollStateChanged(int state) {

	}

	// private void onViewPagerClick(String tag)
	// {
	// start(getContext());
	// }
	// public static void start(Context context) {
	// Intent starter = new Intent(context, WebViewActivity.class);
	// starter.putExtra("name", "详情");
	// starter.putExtra("url", "http://www.jci.edu.cn/index.asp");
	// }

	private void initAdapter() {
		ArrayList<Data> dataArrayList = new ArrayList<Data>();
		dataArrayList.add(new GroupData(0, "g1", "分组1"));
		dataArrayList.add(new ItemData1(1, "gg1", "分组1 数据1"));
		dataArrayList.add(new ItemData1(1, "gg1", "分组1 数据1"));
		dataArrayList.add(new GroupData(0, "g2", "分组2"));
		dataArrayList.add(new ItemData2(2, new GridData(2, "grid1", "02:36",
				"新闻特写：帮助狮子改变命运"), new GridData(2, "grid1", "02:36",
				"《熊猫来了》四姑娘的趣味生活")));
		dataArrayList.add(new ItemData2(2, new GridData(2, "grid1", "02:36",
				"新闻特写：帮助狮子改变命运2"), new GridData(2, "grid1", "02:36",
				"《熊猫来了》四姑娘的趣味生活2")));
		dataArrayList.add(new ItemData2(2, new GridData(2, "grid1", "02:36",
				"新闻特写：帮助狮子改变命运2"), new GridData(2, "grid1", "02:36",
				"《熊猫来了》四姑娘的趣味生活2")));
		dataArrayList.add(new ItemData3(3, "grid1", "02:36", "新闻特写：帮助狮子改变命运"));
		dataArrayList.add(new ItemData3(3, "grid1", "02:36", "新闻特写：帮助狮子改变命运"));
		dataArrayList.add(new ItemData3(3, "grid1", "02:36", "新闻特写：帮助狮子改变命运"));
		LayoutInflater inflater = LayoutInflater.from(getActivity());
		mAdapter = new HomeAdapter(inflater, 4, dataArrayList, mClick);
		mListView.setAdapter(mAdapter);
	}

	View.OnClickListener mClick = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			System.out.println("sssssssssssssss" + v.getTag().toString());
			if (v.getTag() == null) {
				return;
			}
			String[] tags = v.getTag().toString().split(",");
			int pos = Integer.parseInt(tags[0]);
			int index = Integer.parseInt(tags[1]);
			Data item = mAdapter.getItemData(pos, index);
			if (item.mType == 2) {
				GridData itemData = (GridData) item;
				System.out.println("sssssssssssssss" + itemData.info);
			}

		}
	};
}
